<?php

/**
 * FileUpload form.
 *
 * @package    chiks
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class FileUploadForm extends BaseFileUploadForm
{
    public static $lastInstance=null;
  public function configure()
  { 
      self::$lastInstance=$this;
        unset(
          $this['created_at'], $this['updated_at'],$this['original_name']
        );
        
        $this->widgetSchema['name'] = new sfWidgetFormInputFileEditable(array(
            'file_src' => $this->getObject()->getWebPath(),
            'is_image' => true,
            'template' => '<style type="text/css">.edit_image img{ width: 150px; height:100px; }</style><div class="edit_image">%file%<br />%input%<br />%delete% %delete_label%</div>',
            'edit_mode'=>$this->getObject()->getName(),
            'label' => 'Archivo'
        ));
        $this->widgetSchema['banners_list']    = new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'Banner',"renderer_class"=>"sfWidgetFormSelectDoubleList"));
        $this->widgetSchema['promotions_list']    = new sfWidgetFormDoctrineChoice(array('multiple' => false, 'model' => 'Promotion',"add_empty"=>"Ninguno"));
        
        $this->validatorSchema['name'] = new sfValidatorFile(array(
          'required'   => $this->getObject()->getName()?false:true,
          'path'       => sfConfig::get('sf_upload_dir'),
          'mime_types' => 'web_images',
          'max_size'   => '500000'
        ));
        $this->validatorSchema["name_delete"]= new sfValidatorBoolean();
          $this->mergePostValidator(new sfValidatorCallback(array("callback"=>"FileUploadForm::validFile")));
        $this->widgetSchema->setLabels(array(
            'status' => 'Estado',
            'banners_list' => 'Lista de Banners',
            'promotions_list' => 'Lista de Promociones',
        ));
    }
    
    public static function getLastInstance(){
        return self::$lastInstance;
    }
    public static function validFile($validator,$values=array()){
        if($values['name']){
        self::getLastInstance()->getObject()->setOriginalName($values['name']->getOriginalName());
        }
        return $values;
    }
}
